package com.irun.pm.controller;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.irun.pm.interfaces.IEnforcementController;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.mode.Enforcement;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-27
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 现场执法，实例化操作数据库类 向activity提供调用接口
 */
public class EnforcementControllers {

	private IEnforcementController ienforcement;
	public EnforcementControllers(Context context) {
		ienforcement = new EnforcementManager(context);
	}

	public List<Enforcement> getEnforcementList(String name, String date_from, String date_to, String type) {
		/*String selection = "";
		if (!StringUtil.isEmpty(name)) {
			selection += "&eventName=" + name;
		}
		if (StringUtil.isEmpty(date_to)) {
			selection += "&date_to=" + date_to;
		}
		if (!StringUtil.isEmpty(date_from)) {
			selection += "&date_from=" + date_from;
		}*/
		List<Enforcement> list = new ArrayList<Enforcement>();
		String uri = "http://59.61.83.202:8089/query?eventName=" + name + "&date_to=" + date_to + "&date_from=" + date_from;
		System.out.println("uri:" + uri);
		try {
			String data = Utils.getRemoteServicesData(uri);
			System.out.println("data:" + data);
			if (!TextUtils.isEmpty(data)) {
				JSONArray json = new JSONArray(data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject obj = json.getJSONObject(i);
					Enforcement e = new Enforcement();
					e.setId(obj.getString("Id"));
					e.setNativeAddress(obj.getString("eventName"));
					e.setDate(obj.getString("happenDate"));
					e.setPlace(obj.getString("happenPlace"));
					list.add(e);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}

	public DriverInfo getEnforcementById(String id) {
		DriverInfo info = null;
		String uri = "http://59.61.83.202:8089/query?id=" + id;
		try {
			System.out.println("uri:" + uri);
			String data = Utils.getRemoteServicesData(uri);
			System.out.println("data:" + data);
			if (!TextUtils.isEmpty(data)) {
				JSONArray json = new JSONArray(data);
				info = new DriverInfo();
				for (int i = 0; i < json.length(); i++) {
					JSONObject obj = json.getJSONObject(i);
					info.setNativeAddress(obj.getString("eventName"));
					info.setDate(obj.getString("happenDate"));
					info.setAddress(obj.getString("happenPlace"));
					info.setCardNumber(obj.getString("violationCode"));
					info.setName(obj.getString("driverName"));
					info.setSex(obj.getString("driverSex"));
					info.setCard(obj.getString("code"));
					info.setDriverAge(obj.getString("driveCode"));
					info.setNote(obj.getString("information"));
					info.setResult(obj.getString("handleResult"));
					info.setDeal_people(obj.getString("handlePeople"));
					info.setPhotoPath(obj.getString("path"));
				}
			}
		} catch (JSONException e) {
			return null;
		}
		return info;
	}

	public String saveEnforcement(DriverInfo info) {
		StringBuffer url = new StringBuffer("http://59.61.83.202:8089/add");
		url.append("?eventName=").append(info.getNativeAddress())
				.append("&happenDate=").append(info.getDate())
				.append("&happenPlace=").append(info.getAddress())
				.append("&violationCode=").append(info.getCardNumber())
				.append("&driverName=").append(info.getName())
				.append("&driverSex=").append(info.getSex()).append("&code=")
				.append(info.getCard()).append("&driveCode=")
				.append(info.getDriverAge()).append("&information=")
				.append(info.getNote()).append("&handleResult=")
				.append(info.getResult()).append("&handlePeople=")
				.append(info.getDeal_people()).append("&path=")
				.append(info.getPhotoPath());
		System.out.println("url:" + url.toString());
		String result = "";
		try {
			String data = Utils.getRemoteServicesData(url.toString());
			System.out.println("data:" + data);
			JSONArray json = new JSONArray(data);
			for (int i = 0; i < json.length(); i++) {
				JSONObject obj = json.getJSONObject(i);
				result = obj.getString("result");
			}
		} catch (Exception e) {
			result = "";
			e.printStackTrace();
		}
		return result;
	}
}
